package com.im.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;






import com.im.db.DBFactory;
import com.im.entity.Member;

public class MemberDAO {
	
	
	public static String insert(Member member){
		try{
			String sql = "INSERT INTO member(name, email,password,phone) VALUES (?,?,?,?)";
			
			Connection con = DBFactory.getCon();
			PreparedStatement ps = con.prepareStatement(sql);

			ps.setString(1, member.getName());
			ps.setString(2, member.getEmail());
			ps.setString(3, member.getPassword());
			ps.setString(4, member.getPhone());
			
			ps.executeUpdate();
			
			return "Registration Successfull !";
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "please retry!";
	}
	
	public static Member login(String email, String pw){
	
		Member member = null;
		
		try{
			String sql = "SELECT * FROM member WHERE email=? AND password=?";
			
			Connection con = DBFactory.getCon();
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setString(1, email);
			ps.setString(2, pw);
			
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				member = new Member();
				member.setName(rs.getString("name"));
				member.setEmail(rs.getString("email"));
				member.setPassword("<No View>");
				member.setPhone(rs.getString("phone"));
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return member;
	}

	
	
	public static List<Member> listMembers(){
		List<Member> list = null;
		
		return list;
	}
	
	
	
	
}
